<template>
  <ContentWrap >
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="获客编号" prop="obtainGuestNo">
        <el-input
          v-model="queryParams.obtainGuestNo"
          placeholder="请输入获客编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="clientName">
        <el-input
          v-model="queryParams.clientName"
          placeholder="请输入客户名称/单位名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="taxNumber">
        <el-input
          v-model="queryParams.taxNumber"
          placeholder="请输入身份证号/企业税号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>

      <el-form-item label="付款方式" prop="payType">
        <el-select
          v-model="queryParams.payType"
          placeholder="请选择付款方式"
          clearable
          class="!w-240px"
        >
          <el-option v-for="dict in getStrDictOptions(DICT_TYPE.PAY_CHANNEL_CODE)" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="所属大区" prop="payType">
        <el-select
          v-model="queryParams.belongArea"
          placeholder="请选择所属大区"
          clearable
          class="!w-240px"
        >
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.CRM_BELONG_AREA)" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm()"
          v-hasPermi="['crm:obtain-guest:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['crm:obtain-guest:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>

      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
        row-key="id"
        v-loading="loading"
        :data="list"
        :stripe="true"
        :show-overflow-tooltip="true"
    >
    <el-table-column  width="55" />
      <el-table-column label="获客编号" align="center" prop="obtainGuestNo" />
      <el-table-column label="客户类型" align="center" prop="clientType" >
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CRM_CLIENT_TYPE" :value="scope.row.clientType" />
        </template>
      </el-table-column>
      <el-table-column label="客户名称/单位名称" align="center" prop="clientName" />
      <el-table-column label="身份证号/企业税号" align="center" prop="taxNumber" />
      <el-table-column label="业务经理" align="center" prop="businessManagerName" />
      <el-table-column label="付款方式" align="center" prop="payType" >
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.PAY_CHANNEL_CODE" :value="scope.row.payType" />
        </template>
      </el-table-column>
      <el-table-column label="销售类型" align="center" prop="sellType" >
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CRM_SELL_TYPE" :value="scope.row.sellType" />
        </template>
      </el-table-column>
      <el-table-column label="合同类型" align="center" prop="contractType" >
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CRM_CONTRACT_TYPE" :value="scope.row.contractType" />
        </template>
      </el-table-column>
      <el-table-column label="产品类型" align="center" prop="productType" >
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CRM_PRODUCT_TYPE" :value="scope.row.productType" />
        </template>
      </el-table-column>
      <el-table-column label="客户详情地址" align="center" prop="clientDetailAddress" />
      <el-table-column label="所属大区" align="center" prop="belongArea" >
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CRM_BELONG_AREA" :value="scope.row.belongArea" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button link type="primary" @click="openDetail(scope.row.id)"> 详情 </el-button>
          <el-button
            link
            type="primary"
            @click="openForm( scope.row.id)"
            v-hasPermi="['crm:obtain-guest:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['crm:obtain-guest:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

</template>

<script setup lang="ts">
import download from '@/utils/download'
import {ObtainGuestApi, ObtainGuestVO} from '@/api/crm/obtainguest'
import {DICT_TYPE, getIntDictOptions, getStrDictOptions} from "@/utils/dict";

/** 订单获客主 列表 */
defineOptions({ name: 'ObtainGuest' })
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const currentRow = ref({})
const loading = ref(true) // 列表的加载中
const list = ref<ObtainGuestVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  obtainGuestNo: undefined,
  clientType: undefined,
  clientName: undefined,
  taxNumber: undefined,
  clientPhone: undefined,
  businessManagerId: undefined,
  businessManagerName: undefined,
  businessManagerPhone: undefined,
  payType: undefined,
  sellType: undefined,
  contractType: undefined,
  productType: undefined,
  clientAreaId: undefined,
  clientDetailAddress: undefined,
  remark: undefined,
  belongArea: undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const { push } = useRouter()

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ObtainGuestApi.getObtainGuestPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const openForm = (id?: number) => {
  // 修改
  if (typeof id === 'number') {
    push({ name: 'CrmObtainGuestEdit', params: { id } })
    return
  }
    push({ name: 'CrmObtainGuestAdd' })

}
 /** 查看详情 */
const openDetail = (id: number) => {
  push({ name: 'CrmObtainGuestDetail', params: { id } })
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ObtainGuestApi.deleteObtainGuest(id)
    message.success(t('common.delSuccess'))
    currentRow.value = {}
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ObtainGuestApi.exportObtainGuest(queryParams)
    download.excel(data, '订单获客主.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 激活时 */
onActivated(() => {
  getList()
})

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
